<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>作用域</title>
</head>
<body>
<h1>作用域</h1>
<p>
    在js中，对象和函数同样为变量，作用域可以访问变量，对象和函数的集合
</p>
<hr>
<script>
    //a是全局作用域 b是局部作用域
    var a = 10

    function f1() {
        var b = 20
        console.log(a)
        console.log(b)
    }

    f1() //10 20

    function f2() {
        console.log(a)
        console.log(b)
    }

    f2() //10 Uncaught ReferenceError: b is not defined
</script>
<h2>变量提升(只适用于var)</h2>
<script>
    var a = 100

    function f1() {
        var b = 200
        console.log(a)
        console.log(b)
    }

    f1() //100 200
    console.log(a) //100
</script>
</body>
</html>